<?php
/**
 * @author GhostWei
 * @date Sep 15, 2012
 */
class oauth_response_sina extends oauth_api_response {
	
	public function getAvatar() {
		return $this->get('avatar_large', resource_config_avatar::noMedia());
	}
	
	public function getNickname() {
		return $this->get('screen_name');
	}
	
	public function getGender() {
		$gender = 0;
		if($this->get('gender') == 'f') $gender = 2;
		if($this->get('gender') == 'm') $gender = 1;
		return $gender;
	}
	
	public function getUid() {
		return $this->get('id', '');
	}
	
	public function getUser() {
		$user = $this->_rowAuthor();
		$user->id = $this->getUid();
		$user->name = $this->get('name');
		$user->location = $this->get('location');
		$user->description = $this->get('description');
		$user->homepage = $this->get('url');
		$user->avatar = $this->getAvatar();
		$user->gender = $this->getGender();
		$user->tweets = $this->get('statuses_count', 0);
		$user->follows = $this->get('followers_count', 0);
		$user->following = $this->get('friends_count', 0);
		return $user;
	}
	
	public function getTweets() {
		$data = array();
		foreach($this->get('statuses', array()) AS $row) {
			$gender = 0;
			if($row->user->gender == 'f') $gender = 2;
			if($row->user->gender == 'm') $gender = 1;
			$tweet = $this->_rowTweet();
			$tweet->author->id = $row->user->id;
			$tweet->author->name = $row->user->name;
			$tweet->author->location = $row->user->location;
			$tweet->author->description = $row->user->description;
			$tweet->author->homepage = $row->user->url;
			$tweet->author->avatar = $row->user->avatar_large;
			$tweet->author->gender = $gender;
			$tweet->author->tweets = $row->user->statuses_count;
			$tweet->author->follows = $row->user->followers_count;
			$tweet->author->following = $row->user->friends_count;
			
			$tweet->id = $row->mid;
			$tweet->content = $row->text;
			if($row->created_at && $time = @strtotime($row->created_at)) {
				$tweet->timeline = $time;
			}
			if($row->thumbnail_pic && $row->bmiddle_pic && $row->original_pic) {
				$tweet->images = array(
						array(
							'thumb' => $row->thumbnail_pic,
							'middle' => $row->bmiddle_pic,
							'original' => $row->original_pic,
						)
				);
			}
			if($row->source && preg_match('/<a[^>]+href="([^"]+)"[^>]*>([^<]+)<\/a>/s', $row->source, $s)) {
				$tweet->source = array('name' => $s[2], 'url' => $s[1]);
			}
			$data[] = $tweet;
		}
		return $data;
	}
	
	public function getTopics() {
		$data = array();
		foreach($this->get('trends', array()) AS $rows) {
			foreach($rows AS $row) {
				$topic = $this->_rowTopic();
				$topic->name = $row->query;
				$topic->title = $row->name;
				$topic->hot = $row->amount;
				$topic->description = null;
				$data[] = $topic;
			}
		}
		return $data;
	}

	public function getTopicTweetsPaginator(){}
	public function getUserTweetsPaginator(){}
	
}
